// Pipeline Component Library Example
// ----------------------------------
// Colin Bowern (colinbowern@hotmail.com)
// January 2007
//
// Copyright (c) 2007 Colin Bowern
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// * The above copyright notice and this permission notice shall be included in
//   all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#region References
using Microsoft.CommerceServer.Runtime;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
#endregion

namespace RockstarGuys.PipelineComponents
{
    /// <summary>
    /// Represents a shipment associated with a specific order form.
    /// </summary>
    /// <remarks>
    /// For more information see http://msdn2.microsoft.com/en-us/library/microsoft.commerceserver.runtime.orders.shipment.aspx
    /// </remarks>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1035:ICollectionImplementationsHaveStronglyTypedMembers"), DebuggerDisplay("ShipmentId = {ShipmentId}")]
    public class Shipment : Dictionary
    {
        #region Fields
        private SimpleListContainer<ShippingDiscountRecord, IDictionary> shippingDiscounts;
        private SimpleList<int> lineItemIndexes;
        #endregion

        #region Properties
        /// <remarks />
        public Guid ShipmentId
        {
            get { return GetGuidFromStringValue(Shipment.KeyNames.ShipmentId).Value; }
            set { this[Shipment.KeyNames.ShipmentId] = value.ToString(); }
        }

        /// <remarks />
        public string Status
        {
            get { return this[Shipment.KeyNames.Status] as string; }
            set { this[Shipment.KeyNames.Status] = value; }
        }

        /// <remarks />
        public string ShippingAddressId
        {
            get { return this[Shipment.KeyNames.ShippingAddressId] as string; }
            set { this[Shipment.KeyNames.ShippingAddressId] = value; }
        }

        /// <remarks />
        public Guid ShippingMethodId
        {
            get { return GetGuidFromStringValue(Shipment.KeyNames.ShippingMethodId).Value; }
            set { this[Shipment.KeyNames.ShippingMethodId] = value.ToString(); }
        }

        /// <remarks />
        public string ShippingMethodName
        {
            get { return this[Shipment.KeyNames.ShippingMethodName] as string; }
            set { this[Shipment.KeyNames.ShippingMethodName] = value; }
        }

        /// <remarks />
        public string ShipmentTrackingNumber
        {
            get { return this[Shipment.KeyNames.ShipmentTrackingNumber] as string; }
            set { this[Shipment.KeyNames.ShipmentTrackingNumber] = value; }
        }

        #region Totals
        /// <remarks />
        public decimal ShippingDiscountAmount
        {
            get { return GetValue<decimal>(Shipment.KeyNames.ShippingDiscountAmount); }
            set { this[Shipment.KeyNames.ShippingDiscountAmount] = new CurrencyWrapper(value); }
        }

        /// <remarks />
        public decimal ShippingTotal
        {
            get { return GetValue<decimal>(Shipment.KeyNames.ShippingTotal); }
            set { this[Shipment.KeyNames.ShippingTotal] = new CurrencyWrapper(value); }
        }

        /// <remarks />
        public decimal? TaxTotal
        {
            get { return GetValue<decimal?>(Shipment.KeyNames.TaxTotal); }
            set { this[Shipment.KeyNames.TaxTotal] = (value.HasValue ? new CurrencyWrapper(value.Value) : null); }
        }

        /// <remarks />
        public decimal? TaxIncluded
        {
            get { return GetValue<decimal?>(Shipment.KeyNames.TaxIncluded); }
            set { this[Shipment.KeyNames.TaxIncluded] = (value.HasValue ? new CurrencyWrapper(value.Value) : null); }
        }
        #endregion

        /// <remarks />
        public SimpleListContainer<ShippingDiscountRecord, IDictionary> ShippingDiscounts
        {
            get
            {
                if (shippingDiscounts == null)
                {
                    if (this[Shipment.KeyNames.ShippingDiscounts] == null)
                    {
                        this[Shipment.KeyNames.ShippingDiscounts] = new SimpleListClass();
                    }
                    shippingDiscounts = new SimpleListContainer<ShippingDiscountRecord, IDictionary>(this.GetValue<ISimpleList>(Shipment.KeyNames.ShippingDiscounts));
                }
                return shippingDiscounts;
            }
        }

        /// <remarks />
        public SimpleList<int> LineItemIndexes
        {
            get
            {
                if (lineItemIndexes == null)
                {
                    if (this[Shipment.KeyNames.LineItemIndexes] == null)
                    {
                        this[Shipment.KeyNames.LineItemIndexes] = new SimpleListClass();
                    }
                    lineItemIndexes = new SimpleList<int>(this.GetValue<ISimpleList>(Shipment.KeyNames.LineItemIndexes));
                }
                return lineItemIndexes;
            }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Shipment"/> class.
        /// </summary>
        /// <remarks>
        /// The underlying store can be persisted through referencing the InnerList property.
        /// </remarks>
        public Shipment() : base()
        {
            this.ShipmentId = Guid.NewGuid();
            this.ShipmentTrackingNumber = string.Empty;
            this.ShippingAddressId = string.Empty;
            this.ShippingDiscountAmount = decimal.Zero;
            this.ShippingMethodId = Guid.Empty;
            this.ShippingMethodName = string.Empty;
            this.ShippingTotal = decimal.Zero;
            this.Status = string.Empty;
            this.TaxIncluded = decimal.Zero;
            this.TaxTotal = decimal.Zero;
            this[Shipment.KeyNames.ShippingDiscounts] = new SimpleListClass();
            this[Shipment.KeyNames.LineItemIndexes] = new SimpleListClass();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:Shipment"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        [CLSCompliant(false)]
        public Shipment(IDictionary container) : base(container) { }
        #endregion

        #region Nested Types
        /// <summary>
        /// Keys used to access the properties in the dictionary.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes")]
        public struct KeyNames
        {
            /// <remarks />
            public const string ShipmentId = "shipment_id";
            /// <remarks />
            public const string ShippingAddressId = "shipping_address_id";
            /// <remarks />
            public const string ShippingMethodId = "shipping_method_id";
            /// <remarks />
            public const string ShippingMethodName = "shipping_method_name";
            /// <remarks />
            public const string ShipmentTrackingNumber = "shipment_tracking_number";
            /// <remarks />
            public const string ShippingTotal = "_cy_shipping_total";
            /// <remarks />
            public const string ShippingDiscountAmount = "_cy_shipping_discounts_subtotal";
            /// <remarks />
            public const string Status = "shipment_status";
            /// <remarks />
            public const string TaxTotal = "_cy_tax_total";
            /// <remarks />
            public const string TaxIncluded = "_cy_tax_included";
            /// <remarks />
            public const string LineItemIndexes = "ItemIndexes";
            /// <remarks />
            public const string ShippingDiscounts = "_shipping_discounts_applied";
        }
        #endregion
    }
}
